<?php

namespace app\common\model;

use think\Db;
use think\Model;


class AgentSendTips extends Model
{

    

    

    // 表名
    protected $name = 'agent_send_tips';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
		'status_text'
    ];
    
	const STATUS_WAIT = 0;
	const STATUS_DONE = 2;
	const STATUS_DEL = -1;
    
	public static $statusList = [
		self::STATUS_DONE => '已发货',
		self::STATUS_WAIT => '待发货',
		self::STATUS_DEL => '已删除'
	];

	public function getStatusTextAttr($value, $data) {
		$value = $value ? : $data['status'];
		$list = self::$statusList;
		return $list[$value] ?? '';
	}
	
	public function user() {
		return $this->belongsTo('user', 'user_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	public function agent() {
		return $this->belongsTo('agentShop', 'agent_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	public function goods() {
		return $this->belongsTo('goods', 'goods_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	public function spec() {
		return $this->belongsTo('goodsSpec', 'spec_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	/**
	 * 添加发货提醒信息
	 * @param $userId
	 * @param $agentId
	 * @param $goodsId
	 * @param int $specId
	 * @return int|string
	 */
	public static function addRow($userId, $agentId, $goodsId, $specId = 0) {
		return Db::name('agentSendTips')
			->insert([
				'user_id' => $userId,
				'agent_id' => $agentId,
				'goods_id' => $goodsId,
				'spec_id' => $specId,
				'create_time' => time(),
				'update_time' => time()
			]);
	}


}
